<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单投诉ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/bill/billComplain.class.php');

class ajax_billComplain {

    public  function execute () {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']){

                case 1:     //订单投诉查询
                    $this->billComplainList();
                    break;

                case 2:     //添加维权投诉备注
                    $this->addBillComplain();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }


    /**
     * 订单投诉列表
     */
    private function billComplainList() {

        global $G_PGLOB;

        //实例订单投诉类
        $billComplain = new billComplain( $G_PGLOB->DBCA );

        $bill_info_array = $billComplain->billComplainSearch( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $bill_info_array ) {
            foreach($bill_info_array as &$bill) {


                if ($bill['complain_status'] == 1) {

                    $operate = '<a href="javascript: void(0);" onclick="bill.dealComplain(' . $bill['complain_id'] . ');">处理投诉</a>&nbsp;&nbsp;';

                }else{
                    $operate = '';
                }

                if ($bill['complain_status'] == 1) {
                    $status_name = '未处理';
                }else if( $bill['complain_status'] == 2 ){
                    $status_name = '正在处理';
                }else{
                    $status_name = '已处理';
                }
                $result_array['rows'][] = array(
                    'id' => $bill['complain_id'],
                    'cell' => array(
                        $operate,
                        $status_name,
                        $bill['createTime'],
                        $bill['bill_code'],
                        $bill['cust_name'],
                        $bill['mobile'],
                        $bill['complain_content'],
                        $bill['complain_dispose_person'],
                        $bill['complain_dispose_time'],
                        $bill['complain_dispose_remark']
                    )
                );
            }
        }

        echo json_encode($result_array);

        return;
    }


    /**
     * 添加维权投诉备注
     */
    public function addBillComplain(){
        global $G_PGLOB;
        if( !isset($_REQUEST['complain_dispose_remark'],$_REQUEST['complain_id']) || empty($_REQUEST['complain_dispose_remark']) || !quanli_is_int($_REQUEST['complain_id'])){
            returnJsonData('参数错误',400);
        }
        $arg_array = array(
            'complain_id'=>$_REQUEST['complain_id']
        );

        $billComplain = new billComplain();
        $billComplain_array = $billComplain->billComplainSearch( $arg_array );
        if( !$billComplain_array ){
            returnJsonData('维权投诉备注不存在，请刷新页面',404);
        }

        $billComplain_obj = new entity('bill_complain');
        $billComplain_obj->complain_id = $_REQUEST['complain_id'];
        if( !$G_PGLOB->DBCA->fetch( $billComplain_obj)){
            returnJsonData('维权投诉备注不存在，请刷新页面',404);
        }
        $billComplain_obj->complain_status = 3;
        $billComplain_obj->complain_dispose_person = $G_PGLOB->currentUserID;
        $billComplain_obj->complain_dispose_time = $G_PGLOB->sys_datetime;
        $billComplain_obj->complain_dispose_remark = $_REQUEST['complain_dispose_remark'];
        $G_PGLOB->DBCA->save( $billComplain_obj );
        returnJsonData('维权投诉处理成功',200);
    }

}